package com.sacodemel.financeiro.persistencia.dao.impl;

import com.sacodemel.financeiro.persistencia.dao.IConsoleDAO;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ConsoleDAO<T> implements IConsoleDAO<T> {

    private Class<T> clazz;

    /**
     * Construtor padrão.
     * 
     * @param clazz
     *            Classe a ser manipulada por este DAO
     */
    public ConsoleDAO(Class<T> clazz) {

        this.clazz = clazz;

    }

    @Override
    public void executaSQL(String sql) {
        BaseDAO base = new BaseDAO(null);
        Session s = base.getSession();
        Transaction tx = s.beginTransaction();

        SQLQuery procedure = s.createSQLQuery(sql);
        procedure.executeUpdate();

        tx.commit();

    }

    public List<T> listarComCondicoes(String condicoes) throws Exception {

        Session session = null;
        
        try {
            BaseDAO base = new BaseDAO(null);
            session = base.getSession();
            Transaction tx = session.beginTransaction();

            List<T> obj = (List<T>) session.createQuery(
                    "from " + clazz.getSimpleName()+  " " + condicoes).list();


            return obj;

        } catch (Exception e) {
            throw e;
        } finally {
            session.close();
        }



    }
}
